package com.block.common.config;

import cn.hutool.core.util.StrUtil;
import com.block.common.service.LogFactory;
import com.block.common.util.LocalDateUtil;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 
 * @date 2020/11/10 4:19 下午
 */
@Configuration
public class RedissonConfiguration {
    Logger logger = LogFactory.getLog(RedisConfig.class);
    @Value("${spring.redis.redisson.usesingleserver.idleConnectionTimeout}")
    private int idleConnectionTimeout;
    @Value("${spring.redis.redisson.usesingleserver.connectTimeout}")
    private int connectTimeout;
    @Value("${spring.redis.redisson.usesingleserver.timeout}")
    private int timeout;
    @Value("${spring.redis.redisson.usesingleserver.retryAttempts}")
    private int retryAttempts;
    @Value("${spring.redis.redisson.usesingleserver.retryInterval}")
    private int retryInterval;
    @Value("${spring.redis.password}")
    private String password;
    @Value("${spring.redis.host}")
    private String host;
    @Value("${spring.redis.port}")
    private int port;
    @Value("${spring.redis.redisson.usesingleserver.database}")
    private int database;
    @Value("${spring.redis.usessl}")
    private int useSsl;

    @Bean
    public RedissonClient redissonClient() throws IOException {
        logger.info("redisson 开始....{}", LocalDateUtil.getNowByTimeMillis());
        List<String> list = StrUtil.split(host, ",");
        Config config = new Config();
        if (list != null && list.size() > 1) {
            config.useClusterServers().setIdleConnectionTimeout(idleConnectionTimeout);
            config.useClusterServers().setConnectTimeout(connectTimeout);
            config.useClusterServers().setTimeout(timeout);
            config.useClusterServers().setRetryAttempts(retryAttempts);
            config.useClusterServers().setRetryInterval(retryInterval);
            config.useClusterServers().setPassword(password);
            List<String> redisServers = null;
            if (useSsl == 0) {
                redisServers = list.stream().map(s -> "rediss://".concat(s).concat(":").concat(port + "")).collect(Collectors.toList());
            } else {
                redisServers = list.stream().map(s -> "redis://".concat(s).concat(":").concat(port + "")).collect(Collectors.toList());
            }
            String[] serversArray = new String[redisServers.size()];
            config.useClusterServers().addNodeAddress(serversArray);
            config.useClusterServers().setPassword(password);
            config.useSingleServer().setDatabase(database);
        } else {
            config.useSingleServer().setIdleConnectionTimeout(idleConnectionTimeout);
            config.useSingleServer().setConnectTimeout(connectTimeout);
            config.useSingleServer().setTimeout(timeout);
            config.useSingleServer().setRetryAttempts(retryAttempts);
            config.useSingleServer().setRetryInterval(retryInterval);
            config.useSingleServer().setPassword(password);
            if(useSsl==0) {
                config.useSingleServer().setAddress("rediss://".concat(host).concat(":").concat(port + ""));
            }else{
                config.useSingleServer().setAddress("redis://".concat(host).concat(":").concat(port + ""));
            }
            config.useSingleServer().setPassword(password);
            config.useSingleServer().setDatabase(database);
        }

        RedissonClient redissonClient = Redisson.create(config);
        logger.info("redisson 结束....{}", LocalDateUtil.getNowByTimeMillis());
        return redissonClient;
    }
}
